package com.leoman.spinningmachine.util;

import android.app.Activity;
import android.content.SharedPreferences;
import com.leoman.spinningmachine.App;
import com.leoman.spinningmachine.constant.Constant;

/**
 * SP
 * 存本地数据
 */
public class SharedPreferencesUtils {

    static SharedPreferencesUtils instance;
    private SharedPreferences mSharedPreferences;

    public SharedPreferencesUtils() {
        mSharedPreferences = App.getInstance().getSharedPreferences(Constant.SP_NAME, Activity.MODE_PRIVATE);
    }

    // 单例模式
    public static SharedPreferencesUtils getInstance() {
        if (instance == null) {
            synchronized (SharedPreferencesUtils.class) {
                if (instance == null)
                    instance = new SharedPreferencesUtils();
            }
        }
        return instance;
    }

    public void putString(String key, String value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public String getString(String key) {
        return mSharedPreferences.getString(key, "");
    }

    public void putInt(String key, int value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public int getInt(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    public void putBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public Boolean getBoolean(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    public void remove(String key) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.remove(key);
        editor.apply();
    }

    public void clear() {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.clear();
        editor.apply();
    }
}
